package bat.ke.qq.com.service.impl;

import bat.ke.qq.com.bean.Account;
import bat.ke.qq.com.service.AccountService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 源码学院-Fox
 * 只为培养BAT程序员而生
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 */

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    AccountService accountService;

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public  void addAccount(String name, int initMoney) throws Exception {
            String accountName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());

            jdbcTemplate.update("insert INTO account (accountName,user,money) VALUES (?,?,?)",
                    accountName, name, initMoney);

        //((AccountService) AopContext.currentProxy()).queryAccount("fox");
     //  int i=1/0;

        //throw  new Exception("xxx");

       // accountService.updateAccount("fox",1000);

    }

    @Override
    @Transactional
    public List<Account> queryAccount(String name) {
        List<Account> list = jdbcTemplate.queryForList(
        "SELECT * from account where user=?", Account.class, name);

        Arrays.toString(list.toArray());
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int updateAccount(String name, int money) {
        return jdbcTemplate.update(
        "update account set money=money+? where user=?", money, name);
    }

}
